package com.example.controller;

import com.example.common.Result;
import com.example.entity.GreenHouse;
import com.example.entity.Sensors;
import com.example.service.SensorService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@RestController
@RequestMapping("/sensor")
public class SensorController {
    @Resource
    private SensorService sensorService;
    @Resource
    private UserService userService;

    //返回传感器列表
    @PostMapping("/list/sensor")
    public Result listSensors(@RequestBody GreenHouse greenHouse){
        return sensorService.listSensors(greenHouse);
    }

    //新增传感器
    @PostMapping("/add/sensor")
    public Result addSensors(@RequestParam Integer uid, @RequestBody List<Sensors> sensors){
        if(userService.isAdmin(uid)){
            return Result.error("当前用户没有权限");
        }
        return sensorService.addSensors(sensors);
    }

    //删除传感器
    @PostMapping("/delete/sensor")
    public Result deleteSensors(@RequestParam Integer uid,@RequestBody List<Sensors> sensors){
        if(userService.isAdmin(uid)){
            return Result.error("当前用户没有权限");
        }
        return sensorService.deleteSensors(sensors);
    }

    //更新传感器
    @PostMapping("/update/sensor")
    public Result updateSensors(@RequestParam Integer uid,@RequestBody Sensors sensor){
        if(userService.isAdmin(uid)){
            return Result.error("当前用户没有权限");
        }
        return sensorService.updateSensors(Collections.singletonList(sensor));
    }
}
